package com.xdf.whiteaccount.controller;

import com.xdf.whiteaccount.entity.ResponseResult;
import com.xdf.whiteaccount.enums.ResponseEnum;
import com.xdf.whiteaccount.service.CustomizeService;
import com.xdf.whiteaccount.utils.CustomSqlUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: white-account
 * @description:
 * @author: 张柯
 * @create: 2021-06-12 10:16
 **/
@RestController
@RequiresUser
@RequestMapping("/customize")
public class CustomizeController {
    @Autowired
    private CustomizeService customizeService;

    @Autowired
    private CustomSqlUtils customSqlUtils;

    /**
     * 获取当前数据库名称
     *
     * @return
     */
    @GetMapping("/getDatabase")
    public ResponseResult<String> getDatabase() {
        return ResponseResult.<String>builder()
                .success(true)
                .message(ResponseEnum.OK.getName())
                .data(customizeService.getDataBase())
                .build();
    }

    @ApiOperation(value="执行查询SQL", notes="示例：http://localhost:8083/huachongjin/dynamicQuery/querySqlString/select * from bill_type", produces="application/json")
    @GetMapping("/querySqlString/{sql}")
    public ResponseResult querySqlString(@PathVariable String sql){
        return ResponseResult.builder().success(true).message(ResponseEnum.OK.getName()).data(customSqlUtils.queryForList(sql)).build();
    }

    @ApiOperation(value="执行查询SQL", notes="示例：http://localhost:8083/huachongjin/dynamicQuery/querySqlPost。传入参数：{sql:select * from bill_type}", produces="application/json")
    @PostMapping("/querySqlPost")
    public ResponseResult querySqlStringPost(@RequestBody Map<String,String> params){
        String sql = params.get("sql");
        return ResponseResult.builder().success(true).message(ResponseEnum.OK.getName()).data(customSqlUtils.queryForList(sql)).build();
    }

    @ApiOperation(value="调用存储过程进行查询", notes="传入参数格式：[\"usp_getBillType8Storage\",\"原料仓库\"]", produces="application/json")
    @PostMapping("/callProc")
    public ResponseResult callProc(@RequestBody List<String> procAndParams){
        return ResponseResult.builder().success(true).message(ResponseEnum.OK.getName()).data(customSqlUtils.queryForListCallProc(procAndParams)).build();
    }

    @ApiOperation(value="可执行新增、修改、删除语句", notes="传入拼接了参数的SQL", produces="application/json")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody String sql){
        return ResponseResult.builder().success(true).message(ResponseEnum.OK.getName()).data(customSqlUtils.update(sql)).build();
    }
}
